var e = require("../utils/util.js"),
  t = require("../comm/local"),
  r = require("./dfeature"),
  i = require("../comm/packet"),
  n = require("../comm/rmodel").ticketM,
  a = require("../comm/punish"),
  o = require("../comm/rcode"),
  u = null,
  l = null,
  c = null;
function s(n, a, o, c, s, I) {
  try {
    var f,
      d = r.initReqConfig(a),
      D = r.initReqFeatureObj(n, a, o),
      k = (function (n, a, o, u) {
        var l = "";
        e.isEmpty(n.channel) || (l = n.channel);
        var c = r.initContent(n, a, u);
        return i.encryptReqPacket(c, l, o, t.tk);
      })(d, D.featureObj, D.uuid, c),
      m = -1;
    u.request(
      k,
      function (r) {
        try {
          var i = "",
            n = 0;
          if (200 === r.statusCode) {
            var a = r.resp;
            if (0 == (m = a.ret)) {
              var o = a.data.resp;
              if ((o.extraInfo && o.extraInfo[0], (m = o.ret) >= 0)) {
                var u = o.type,
                  c = o.token,
                  d = "";
                o.extraIds && o.extraIds[0] && (d = o.extraIds[0]),
                  (i = o.ticketID),
                  (n = o.overtime),
                  (m = 0);
                var D = { ticketID: i, lifeTime: n, openID: d };
                return (
                  u >= 0 && !e.isEmpty(c) && t.setLocal(t.tTokenKey(l.salt), c),
                  u >= 0 &&
                    !e.isEmpty(i) &&
                    t.setLocal(t.ticketKey(l.salt), {
                      ticketID: i,
                      expire: Date.now() + 1e3 * n,
                      openID: d,
                    }),
                  void s({ ret: m, data: D })
                );
              }
            }
          }
          (m = m), (f = new Error("request ticket fail"));
        } catch (e) {
          (m = -102), (f = e);
        }
        I({ ret: m, err: f });
      },
      function (e) {
        I({ ret: -101, err: new Error("request fail") });
      },
      l.apiUrl,
    );
  } catch (e) {
    I({ ret: -103, err: e });
  }
}
function I(e, r, i) {
  var u = t.tPunishKey(r.salt);
  if (a.isLmitByTimes(u.timesk) && a.isLimitByHalfHour(u.timesk))
    return (n.ret = o.ERROR_BIZ_TIMES_LIMIT), (n.ticketID = ""), void i(n);
  if (a.isLimitByFaild(u.faildk, u.timesk))
    return (n.ret = o.ERROR_BIZ_FAILD_LIMIT), (n.ticketID = ""), void i(n);
  var l = new Date().getTime();
  a.saveTimes(u.timesk, l),
    e.obtainFeature(function (e) {
      for (var o = [], l = [], c = 0; c < e.length; c++) {
        var I = e[c];
        0 != I.ret ? (l.push(I), (o[c] = void 0)) : (o[c] = I.res);
      }
      if (l.length > 0)
        for (var d = 0; d < l.length; d++) {
          var D = l[d];
          t.cacheError(D.ret, D.res);
        }
      var k = t.getErrors(),
        m = f(!1);
      s(
        null != m ? m.ticketID : null,
        r,
        o,
        k,
        function (e) {
          (n.ret = 0),
            (n.data.ticketID = e.data.ticketID),
            null != e.data.openID &&
              null != e.data.openID &&
              (n.data.openID = e.data.openID),
            a.delByKey(u.timesk),
            a.delByKey(u.faildk),
            i(n);
        },
        function (e) {
          (n.ret = e.ret), (n.ticketID = ""), i(n), a.saveFaild(u.faildk);
        },
      );
    });
}
function f(e) {
  var r;
  try {
    r = t.getLocal(t.ticketKey(l.salt));
  } catch (e) {}
  if (r && r.ticketID && r.ticketID.length > 0) {
    var i = r.expire;
    if (!e || i > Date.now()) return r;
  }
  return null;
}
module.exports = {
  initDfp: function (e, t, r, i) {
    (u = e), (l = r), (c = t);
    var a = f(!0);
    if (null != a)
      return (
        (n.ret = 0),
        (n.data.ticketID = a.ticketID),
        null != a.openID && null != a.openID && (n.data.openID = a.openID),
        void i(n)
      );
    setTimeout(function () {
      I(c, l, i);
    }, 500);
  },
  getDfpTicket: function (e) {
    var t = f(!0);
    if (null != t) {
      var r = t.ticketID;
      return (
        (n.ret = 0),
        (n.data.ticket = r),
        null != t.openID && null != t.openID && (n.data.openID = t.openID),
        void e(n)
      );
    }
    setTimeout(function () {
      I(c, l, e);
    }, 500);
  },
};
